from database_lib.testdb.test_model import TestModel
from decorator import close_db_connection, update_db_operation


class ApiCollection(TestModel):
    def __init__(self):
        self.session = self.session()

    @close_db_connection
    def select_case_count_by_service(self, service):
        query = self.session.query(self.ApiCollection).filter_by(service=service).count()
        return query

    def select_api_count_by_service(self, service):
        count = self.session.query(self.ApiCollection.url)\
            .distinct(self.ApiCollection.url).filter_by(service=service).count()
        return count

    @update_db_operation
    def delete_data_by_service(self, service, api_name, tag, documentation, url):
        self.session.query(self.ApiCollection).filter_by(service=service, api_name=api_name, tag=tag, url=url,
                                                         documentation=documentation).delete()

    @update_db_operation
    def add_api_collection(self, service, api_name, tag, documentation, url):
        self.delete_data_by_service(service, api_name, tag, documentation, url)
        data = self.ApiCollection(service=service, api_name=api_name, tag=tag, documentation=documentation, url=url)
        self.session.add(data)